package com.dream.common;
import com.dream.model.SysUser;
import com.dream.model.Sys_Dict;
import com.dream.system.service.IDict;
import java.io.File; 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map; 
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

@Component("springInit")
@SuppressWarnings("all")
public class SpringInit extends HttpServlet
{

  @Autowired
  private IDict dictServiceImpl;
  private static final long serialVersionUID = 1L;

  public void init(ServletConfig config)
    throws ServletException
  {
    super.init(config);
    ServletContext servletContext = config.getServletContext();
    WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    AutowireCapableBeanFactory autowireCapableBeanFactory = webApplicationContext.getAutowireCapableBeanFactory();
    DataInit.WEB_APP_REAL_PATH = config.getServletContext().getRealPath(File.separator);
    autowireCapableBeanFactory.configureBean(this, "dictServiceImpl");
    ServletContext application = getServletContext();

    Sys_Dict dict = new Sys_Dict();
    dict.setStart(0);
    dict.setLimit(10000);
    Map map = this.dictServiceImpl.getDict(dict);
    List list = (List)map.get("list");
    List list1 = this.dictServiceImpl.getDisnetctDict(dict);
    for (int i = 0; i < list1.size(); i++) {
      String str = (String)list1.get(i);
      List listsd = new ArrayList();
      for (int j = 0; j < list.size(); j++) {
        if (str.equals(((Sys_Dict)list.get(j)).getDictSign())) {
          listsd.add((Sys_Dict)list.get(j));
        }
      }
      application.setAttribute(str, listsd);
    }
    try {
      getListUploadUrl(DataInit.WEB_APP_REAL_PATH + "config" + File.separator + "upload.xml");
      System.out.println("上传文件路径为:" + DataInit.UPLOADURL);
    }
    catch (DocumentException e) {
      e.printStackTrace();
      System.out.println("------------加载上传配置文件失败!-------------------");
    }
    try {
      DataInit.listAdmin = getListTerm(DataInit.WEB_APP_REAL_PATH + "config" + File.separator + "admin.xml");
      System.out.println("超级管理员路径 为:" + DataInit.UPLOADURL);
    }
    catch (DocumentException e) {
      e.printStackTrace();
      System.out.println("------------加载超级管理员失败-------------------");
    }
  }

  public static List<SysUser> getListTerm(String path)
    throws DocumentException
  {
    List listAdmin = new ArrayList();
    File f = new File(path);
    SAXReader reader = new SAXReader();

    Document doc = reader.read(f);
    Element root = doc.getRootElement();

    for (Iterator i = root.elementIterator("user"); i.hasNext(); ) {
      Element foo = (Element)i.next();
      SysUser sys_user = new SysUser();
      sys_user.setTable_id(foo.elementText("table_id"));
      sys_user.setTell(foo.elementText("tell"));
      sys_user.setUserPwd(foo.elementText("userPwd"));
      listAdmin.add(sys_user);
    }
    return listAdmin;
  }

  public static void getListUploadUrl(String path)
    throws DocumentException
  {
    File f = new File(path);
    SAXReader reader = new SAXReader();

    Document doc = reader.read(f);
    Element root = doc.getRootElement();

    String str = System.getProperties().getProperty("os.name");
    boolean b = StringUtils.containsIgnoreCase(str, "windows");

    for (Iterator i = root.elementIterator("system"); i.hasNext(); ) {
      Element foo = (Element)i.next();

      if (b)
        DataInit.UPLOADURL = foo.elementText("windows");
      else
        DataInit.UPLOADURL = foo.elementText("linux");
    }
  }
}